package com.hnevc.androiduiclassone10;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    List<Team> teamList;//记录所有球队的信息列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();



        ListView listView = findViewById(R.id.lv_teams);

        MyTeamAdapter adapter = new MyTeamAdapter();

        listView.setAdapter(adapter);



        Toast.makeText(this
                , teamList.size() +""
                , Toast.LENGTH_SHORT).show();
    }

    private void initData() {

        Gson gson = new Gson();
        Type listType = new TypeToken<List<Team>>(){}.getType();

        InputStream in = getResources().openRawResource(R.raw.data);
        Reader reader = new InputStreamReader(in);

        teamList = gson.fromJson(reader,listType );

    }

    class MyTeamAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return teamList.size();
        }

        @Override
        public Object getItem(int position) {
            return teamList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(MainActivity.this,
                                    R.layout.list_item,
                                    null);
            //+
            TextView tvZuhao = view.findViewById(R.id.tv_zuhao);
            tvZuhao.setText(teamList.get(position).getZuhao());
            //+
            Log.e("TAG", "getView: "+"http://www.chashenjia.com/images/gq/"
                    +teamList.get(position).getQiudui()+".jpg" );
            ImageView ivTupian = view.findViewById(R.id.iv_tupian);
            switch (teamList.get(position).getQiudui()){
                case "aodaliya":
                    ivTupian.setImageResource(R.drawable.aidaliya);
                    break;
                case "agenting":
                    ivTupian.setImageResource(R.drawable.agenting);
                    break;
                case  "aiji":
                    ivTupian.setImageResource(R.drawable.aiji);
                    break;
                case "banama":
                    ivTupian.setImageResource(R.drawable.banama);
                    break;
                case "baxi":
                    ivTupian.setImageResource(R.drawable.baxi);
                    break;


            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转
                    Intent intent = new Intent(MainActivity.this,TeamActivity.class;
                    startActivities(intent);
                }
            });
            Picasso.with(MainActivity.this)
                    .load("www.chashenjia.com/images/gq/"
                            +teamList.get(position).getQiudui()+".jpg")
                    .error(R.drawable.baxi)
                    .into(ivTupian);

            return view;
        }
    }
}
